package SwingView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import JDBC.InstructorAssignmentActivities;
import JDBC.InstructorQuizActivities;
import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingModel.CmsQuiz;

public class CmsWeightInputDialog {
	
	public static double showInputDialog( final Double currentWeight){
		List<CmsQuiz> quizzes = InstructorQuizActivities.getListOfQuiz(CmsPresenter.user.getSelectedCourse().getCourseID());
		List<CmsAssignment> asgms = InstructorAssignmentActivities.getListOfAssignments(CmsPresenter.user.getSelectedCourse().getCourseID());
		double weight = currentWeight.doubleValue();
		
		JPanel jpMain = new JPanel(new BorderLayout());
		jpMain.setOpaque(false);
		
		JPanel jpExist = new JPanel(new GridBagLayout());
		jpExist.setOpaque(false);
		jpExist.setBorder(BorderFactory.createTitledBorder("Existing Assignments/Quizzes"));
		jpMain.add(jpExist,BorderLayout.CENTER);
		
		JPanel jpNew = new JPanel(new GridBagLayout());
		jpNew.setOpaque(false);
		jpNew.setBorder(BorderFactory.createTitledBorder("Select Weight"));
		jpMain.add(jpNew,BorderLayout.SOUTH);
		
		int index = 0;
		double totalWeight = 0.0;
		
		//ADD ASGMS
		for(CmsAssignment asgm : asgms){
			JLabel jlTitle = new JLabel(asgm.getAsgmTitle());
			jlTitle.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = index;
			c.anchor = GridBagConstraints.LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			jpExist.add(jlTitle,c);
			
			JLabel jlWeight = new JLabel(asgm.getPercentWeight() + "%");
			jlWeight.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 20));
			jlWeight.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			c = new GridBagConstraints();
			c.gridx = 1;
			c.gridy = index;
			c.anchor = GridBagConstraints.LINE_END;
			c.fill = GridBagConstraints.HORIZONTAL;
			jpExist.add(jlWeight,c);
			
			totalWeight += asgm.getPercentWeight();
			index++;
		}
		
		//ADD QUIZZES
		for(CmsQuiz quiz : quizzes){
			JLabel jlTitle = new JLabel(quiz.getQuizTitle());
			jlTitle.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = index;
			c.anchor = GridBagConstraints.LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			jpExist.add(jlTitle,c);
			
			JLabel jlWeight = new JLabel(quiz.getPercentWeight() + "%");
			jlWeight.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			jlWeight.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 20));
			c = new GridBagConstraints();
			c.gridx = 1;
			c.gridy = index;
			c.anchor = GridBagConstraints.LINE_END;
			c.fill = GridBagConstraints.HORIZONTAL;
			jpExist.add(jlWeight,c);
			
			totalWeight += quiz.getPercentWeight();
			index++;
		}
		
		//ADD TOTAL:
		JLabel jlTitle = new JLabel("TOTAL WEIGHT: ");
		jlTitle.setOpaque(true);
		jlTitle.setBackground(Color.GRAY);
		jlTitle.setForeground(Color.WHITE);
		jlTitle.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = index;
		c.anchor = GridBagConstraints.LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		jpExist.add(jlTitle,c);
		
		JLabel jlWeight = new JLabel(totalWeight + "%");
		jlWeight.setOpaque(true);
		jlWeight.setBackground(Color.GRAY);
		jlWeight.setForeground(Color.WHITE);
		jlWeight.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 20));
		jlWeight.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = index;
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		jpExist.add(jlWeight,c);
		
		final double TOTALWEIGHT = totalWeight;
		
		//ADD WEIGHT TEXT FIElD
		JLabel jlCurrentWeight = new JLabel("Current Selected Weight: ");
		jlCurrentWeight.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		jlCurrentWeight.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		jpNew.add(jlCurrentWeight,c);
		
		final JTextField jtfWeight = new JTextField(""+currentWeight);
		jtfWeight.setColumns(6);
		jtfWeight.setEditable(false);
		jtfWeight.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,0,0);
		jpNew.add(jtfWeight,c);
		
		//ADD WEIGHT TEXT FIElD
		JLabel jlPc = new JLabel("%");
		jlPc.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 0;
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		jpNew.add(jlPc,c);
		
		
		//ADD SLIDER
		final JSlider jsWeight = new JSlider(JSlider.HORIZONTAL,
                0, (int) (100.0 - totalWeight + currentWeight), currentWeight.intValue());
		jsWeight.setPaintTicks(true);
		jsWeight.setMajorTickSpacing(5);
		jsWeight.setMinorTickSpacing(1);
		jsWeight.setPaintLabels(true);
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 3;
		c.anchor = GridBagConstraints.LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,0,20);
		jpNew.add(jsWeight,c);

		
		//ADD WEIGHT TEXT FIElD
		JLabel jlUpTotal = new JLabel("Updated Total Weight: ");
		jlUpTotal.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		jlUpTotal.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		jpNew.add(jlUpTotal,c);
		
		final JTextField jtfTotal = new JTextField("" + (totalWeight));
		jtfTotal.setEditable(false);
		jtfTotal.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		jtfTotal.setColumns(6);
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 2;
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0,10,0,0);
		jpNew.add(jtfTotal,c);
		
		//ADD WEIGHT TEXT FIElD
		JLabel jlPc2 = new JLabel("%");
		jlPc2.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 2;
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		jpNew.add(jlPc2,c);
		
		
		jsWeight.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				jtfWeight.setText(""+jsWeight.getValue());
				jtfTotal.setText("" + (jsWeight.getValue() - currentWeight + TOTALWEIGHT));
			}
			
		});
		
		
		JOptionPane.showMessageDialog(CmsPresenter.mainView, jpMain);
		
		return Double.parseDouble(jtfWeight.getText());
	}
}
